using System.Collections.Generic;
using System.Linq;
using System;

namespace MyUtils
{
	public class DictionaryObject : IDictionaryObject
	{
		readonly Dictionary<string, object> _dic = new Dictionary<string, object>();

		public object GetValue(string propertyName)
		{
			object value;
			_dic.TryGetValue(propertyName, out value);
			return value;
		}

		public void SetValue(string propertyName, object value)
		{
			_dic[propertyName] = value;
		}

		public object this[string propertyName]
		{
			get { return GetValue(propertyName); }
			set { SetValue(propertyName, value); }
		}

		public T Get<T>(string propertyName)
		{
			return (T)GetValue(propertyName);
		}

		public T Set<T>(string propertyName, T value)
		{
			SetValue(propertyName, value);
			return value;
		}

		public T Get<T>(string propertyName, Func<T> factory) where T : class
		{
			return Get<T>(propertyName) ?? Set(propertyName, factory());
		}
	}
}